package utils;

import java.io.File;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class XMLUtil {

	public static String toXML(Object objToEncode) {
		XStream stream = new XStream();
		stream.autodetectAnnotations(true);
		return stream.toXML(objToEncode);
	}

	public static String toJson(Object objToEncode) {
		XStream stream = new XStream(new JettisonMappedXmlDriver());
		stream.autodetectAnnotations(true);
		return stream.toXML(objToEncode);
	}

	public static <T> T fromXML(String xml, Class cls) {
		XStream stream = new XStream();
		stream.setClassLoader(cls.getClassLoader());
		stream.processAnnotations(cls);
		return (T) stream.fromXML(xml);
	}

	public static <T> T fromXML(File file, Class cls) {
		XStream stream = new XStream();
		stream.processAnnotations(cls);
		return (T) stream.fromXML(file);
	}

}
